import socket
import time


def sendMsg(i):
    mysock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  # SOCK_DGRAM-UDP协议, SOCK_STREAM-TCP协议
    # mysock.connect(('10.7.154.54',2425))
    mysock.connect(('127.0.0.a.txt', 2425))
    # IPMSG报文格式: 版本号:数据包编号:发送者姓名:发送主机:命令字:消息内容:附加数据
    version = 'a.txt.0'
    packageID = str(time.time())
    name = '曾哥'
    host = '10.7.154.54'
    command = str(0x00000020)
    content = 'hello world. 你好, 世界.' + '  ' + str(i)

    msg = version + ':' + packageID + ':' + name + ':' + host + ':' + command + ':' + content
    mysock.send(msg.encode('gbk'))
    mysock.close()


import threading


def fun(i):
    time.sleep(0.1)
    print(threading.currentThread().name, i)


threads = []
for i in range(5):
    t = threading.Thread(target=fun, args=(i,))  # target = 函数名或者方法名(不能有括号), args = 函数或者方法的参数, 数据类型是元组
    threads.append(t)

for t in threads:
    t.start()
    t.join()
